﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XiTi
{
    public partial class QutstionForm : Form
    {
        Encoding utf8 = new UTF8Encoding(false);
        List<Question> qs;
        int p = 0;
        string tmp;
        bool CoreWebView2InitializationCompleted = false;

        public QutstionForm(List<Question> qlist)
        {
            qs = qlist;
            InitializeComponent();
            WebView2Helper.SetWebView2ExecutableFolder(webView21, "F:\\WebView2");
            webView21.EnsureCoreWebView2Async();
            
            cb_qs.Items.Clear();
            for(int i=0; i<qs.Count; i++)
            {
                cb_qs.Items.Add(i);
            }
            cb_qs.SelectedIndex = 0;
            cb_qs.SelectedIndexChanged += cb_qs_SelectedIndexChanged;
            
        }
        private void QutstionForm_Load(object sender, EventArgs e)
        {
            tmp = File.ReadAllText("tmp.html", utf8);
            tabControl1.SelectedIndex = 0;
            whiteBoardControl1.Close += WhiteBoardControl1_Close;
        }

        private void WhiteBoardControl1_Close(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
        }


        private async void bt_jiangjie_Click(object sender, EventArgs e)
        {
            MemoryStream stream = new MemoryStream();
            await webView21.CoreWebView2.CapturePreviewAsync(Microsoft.Web.WebView2.Core.CoreWebView2CapturePreviewImageFormat.Png, stream);
            var bmp = Bitmap.FromStream(stream);
            whiteBoardControl1.setImg(bmp);
            tabControl1.SelectedIndex = 1;
        }

        private void bt_放大字体_Click(object sender, EventArgs e)
        {
            webView21.ZoomFactor += 0.1;
        }

        private void bt_缩小字体_Click(object sender, EventArgs e)
        {
            webView21.ZoomFactor -= 0.1;
        }

        private void bt_放大图片_Click(object sender, EventArgs e)
        {
            webView21.ExecuteScriptAsync("SetImageSize(0.1)");
        }

        private void bt_缩小图片_Click(object sender, EventArgs e)
        {
            webView21.ExecuteScriptAsync("SetImageSize(-0.1)");
        }

        private async void bt_shotScreen_Click(object sender, EventArgs e)
        {
            string FilePath = "pic";
            if (!System.IO.Directory.Exists(FilePath))
            {
                System.IO.Directory.CreateDirectory(FilePath);
            }
            MemoryStream stream = new MemoryStream();
            await webView21.CoreWebView2.CapturePreviewAsync(Microsoft.Web.WebView2.Core.CoreWebView2CapturePreviewImageFormat.Png, stream);
            var bmp = Bitmap.FromStream(stream);

            string FileName = DateTime.Now.Ticks.ToString() + ".png";
            bmp.Save(System.IO.Path.Combine(FilePath, FileName));
            System.Diagnostics.Process.Start("Explorer", "/select," + System.IO.Path.Combine(FilePath, FileName));
        }

        private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                CoreWebView2InitializationCompleted = true;
                string html2 = tmp.Replace("#question#", JsonHelper.question2Html(qs[p]));
                webView21.NavigateToString(html2);
            }
            else
            {
                this.Text = e.InitializationException.Message;
            }
        }

        private void bt_pre_Click(object sender, EventArgs e)
        {
            if (p > 0)
            {
                p--;
                cb_qs.SelectedIndex = p;
            }
        }

        private void bt_next_Click(object sender, EventArgs e)
        {
            if (p < qs.Count -1)
            {
                p++;
                cb_qs.SelectedIndex = p;
            }
        }

        private void cb_qs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CoreWebView2InitializationCompleted)
            {
                try
                {
                    p=cb_qs.SelectedIndex;
                    string html2 = tmp.Replace("#question#", JsonHelper.question2Html(qs[p]));
                    webView21.NavigateToString(html2);
                }
                catch(Exception ex)
                {
                    lb_msg.Text = ex.Message;
                }
            }
        }
    }
}
